import java.io.*;
import java.util.zip.*;
import java.util.Scanner;
import java.io.File;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.imageio.ImageIO;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

public class CreateShortcuts{
	CreateShortcuts() {
	}

	 public static String createNewFolder() {
         String path = new String();
                path = System.getProperty("user.home") + "/Desktop";
                File file = new File(path + "/ShortcutsToWebsites");
                file.mkdir();

                return file.getName();
        }

	private static final String WINDOWS_DESKTOP = "Desktop";

	public static String getWindowsCurrentUserDesktopPath() { // return the
																// current user
																// desktop path
		return System.getenv("userprofile") + "/" + WINDOWS_DESKTOP + "/"
				+ createNewFolder();
	}

	public static void createInternetShortcutOnDesktop(String name,
			String target) throws IOException {
		String path = getWindowsCurrentUserDesktopPath() + "/" + name + ".URL";
		createInternetShortcut(name, path, target, "");
	}

	public static void createInternetShortcut(String name, String where,
			String target, String icon) throws IOException {
		FileWriter fw = new FileWriter(where);
		fw.write("[InternetShortcut]\n");
		fw.write("URL=" + target + "\n");
		if (!icon.equals("")) {
			fw.write("IconFile=" + icon + "\n");
		}
		fw.flush();
		fw.close();
	}
	public static void downloadPictures(String target)throws Exception {
		    	
        URL url = new URL(target);
        URLConnection connection = url.openConnection();
        java.io.InputStream is = connection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        HTMLEditorKit htmlKit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
        HTMLEditorKit.Parser parser = new ParserDelegator();
        HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
        parser.parse(br, callback, true);

        for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {
            javax.swing.text.AttributeSet attributes = iterator.getAttributes();
            String imgSrc = (String) ((javax.swing.text.AttributeSet) attributes).getAttribute(HTML.Attribute.SRC);

            if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
                try {
                    downloadImage(target, imgSrc);
                    
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
        if(true)
        {
        	System.out.println("Ready");
        }
    }
	 
	private static void downloadImage(String url, String imgSrc) throws IOException {

	    	
	    	String path=new String();
	    	path = System.getProperty("user.home") + "/Desktop";
	 
	        File dir=new File(path+"/Pictures");
	        dir.mkdir();
	        String newPath=new String();
	        newPath=dir.getAbsolutePath();
	        
	    	BufferedImage image = null;
	        try {
	            if (!(imgSrc.startsWith("http"))) {	
	                url = url + imgSrc;
	            } else {
	                url = imgSrc;
	            }
	            imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
	            String imageFormat = null;
	            imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
	            String imgPath = null;
	            imgPath =newPath+"/"+imgSrc+"";
	            URL imageUrl = new URL(url);
	            image = ImageIO.read(imageUrl);	
	            if (image != null) {
	                File file = new File(imgPath);
	                ImageIO.write(image, imageFormat, file);
	            }
	        } catch (Exception ex) {
	            ex.printStackTrace();
	        }
	    }
	
	public static void main(String[] args) throws Exception {

		String target;
		Scanner sc = new Scanner(System.in);
		String name;
		while (true) {
			System.out.println("Name: ");
			name = sc.next();
			if (name.equals("0") == true) {
				break;
			}
			System.out.println("Website: ");
			target = sc.next();
			CreateShortcuts.createInternetShortcutOnDesktop(name, target);
			CreateShortcuts.downloadPictures(target);
		}
		sc.close();
      
      public class createZip{
    public static void main(String[] args) 	throws Exception { 
        // input file 
        FileInputStream in = new FileInputStream("C:\\Users\\Ico\\Desktop\\Untitled.jpg");

        // out put file 
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream("C:\\Users\\Ico\\Desktop\\Icata.zip"));

        // name the file inside the zip  file 
        out.putNextEntry(new ZipEntry("Untitled.jpg")); 

        // buffer size
        byte[] b = new byte[1024];
        int count;

        while ((count = in.read(b)) > 0) {
            System.out.println();
            out.write(b, 0, count);
        }
        out.close();
        in.close();
    }
}

	}
}
